#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <vector>
#include <cstdio>
#include<assert.h>

using namespace std;
#define NUM 10

class ThreadDate
{
public:
    int number;
    pthread_t tid;
    char buffer[64];
};

class TheadRet
{
public:
    int exit_code;
    int exit_sign;
};

void *start_routine(void *args)
{

    ThreadDate *td = static_cast<ThreadDate *>(args);
    cout << td->buffer << "create success : " << td->tid << endl;
    //int i = 10/0;
    sleep(5);
}

int main()
{
    vector<ThreadDate *> threads;

    cout<<"main pid is : "<<getpid()<<endl;

    //sleep(3);

    for (int i = 0; i < NUM; i++)
    {
        ThreadDate *td = new ThreadDate();
        td->number = i + 1;
        snprintf(td->buffer, sizeof td->buffer, "%s:%d", "thread", td->number);
        pthread_create(&td->tid, nullptr, start_routine, td);
        threads.push_back(td);
        //sleep(1);
    }

    //while(true){sleep(1);}
    // for (auto &iter : threads)
    // {
    //     cout << "create thread: " << iter->buffer << " : " << iter->tid << " success " << endl;
          //sleep(1);
    // }

    //sleep(10);
    //pthread_cancel可以取消线程
    // for(int i=0;i<threads.size();i++)
    //     pthread_cancel(threads[i]->tid);


    void* ret = nullptr;
    for(auto &iter : threads)
    {
        int n = pthread_join(iter->tid,(void**)&ret);
        assert(n==0);
        cout<<"join: "<<iter->number<<" exit code: "<<(long long )ret<<endl;
        delete iter;
    }


    //sleep(5);


    return 0;
}